package com.it.park.controller.expose;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.QuestionAnswerService;
import com.it.park.client.service.QuestionOperationService;
import com.it.park.client.service.QuestionService;
import com.it.park.client.service.QuestionTypeService;
import com.it.park.client.service.SensitiveWordService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.common.SessionUtil;
import com.it.park.constant.Constant;
import com.it.park.model.Question;
import com.it.park.model.QuestionAnswer;
import com.it.park.model.QuestionOperation;
import com.it.park.model.QuestionType;
import com.it.park.model.SensitiveWord;
import com.it.park.rms.client.model.User;

/**
 * @author Administrator
 *问题交流
 */
@Controller
@RequestMapping("/expose/question")
public class ExposeQuestionController {
	private String rootPath=Constant.EXPOSE_PATH;

	private List<Question> questions =	null;
	
	private Question question = null;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	@Autowired
	private QuestionOperationService questionOperationService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	private Page page = null;
	
	
	@RequestMapping("questionByTypeList")
	public String questionByTypeList(HttpServletRequest request){
			String pageNow = request.getParameter("pageNow");
			String questionTypeId = request.getParameter("id");
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("questionTypeId", questionTypeId);
			Integer total = questionService.selectTotal(map);
			if(pageNow != null){
				page = new Page(Integer.parseInt(pageNow),total,10);
			}else{
				page = new Page(1,total,10);
			}
			map.put("startPos", page.getStartPos());
			map.put("pageSize", page.getPageSize());
			questions = questionService.selectPagination(map);
			request.setAttribute("total", total);
			request.setAttribute("page", page);
			request.setAttribute("questions", questions);
			
			QuestionType questionType = questionTypeService.selectByPrimaryKey(Integer.parseInt(questionTypeId));
			request.setAttribute("questionType", questionType);
			return rootPath+"question/questionByTypeList";
	}
	
	
	/**
	 *单个问题详情 
	 */
	@RequestMapping("questionDetail")
	public String questionDetail(@RequestParam("id") Integer id,HttpServletRequest request){
		 question = questionService.selectByPrimaryKey(id);
		 question.setQuestionClick(question.getQuestionClick()+1);
		 questionService.updateByPrimaryKeySelective(question);
		request.setAttribute("question", question);
		
		//问题回答展示
		String pageNow = request.getParameter("pageNow");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("questionId", id);
		Integer total = questionService.selectTotal(map);
		if(pageNow != null){
			page = new Page(Integer.parseInt(pageNow),total,10);
		}else{
			page = new Page(1,total,10);
		}
		map.put("startPos", page.getStartPos());
		map.put("pageSize", page.getPageSize());
		List<QuestionAnswer> questionAnswers=questionAnswerService.selectPagination(map);
		request.setAttribute("questionAnswers", questionAnswers);
		
		//问题分类
		QuestionType questionType = questionTypeService.selectByPrimaryKey(question.getQuestionTypeId());
		request.setAttribute("questionType", questionType);
		
		return rootPath+"question/questionDetail";
	}
	
	/**
	 *单个问题回答 
	 */
	@RequestMapping("questionAnswerDetail")
	public String questionAnswerDetail(@RequestParam("id") Integer id,HttpServletRequest request){
		question = questionService.selectByPrimaryKey(id);//当前页
		request.setAttribute("question", question);
		return rootPath+"question/questionDetail";
	}
	
	/**
	 * 问题收藏、关注
	 */
	@RequestMapping("questionOperation")
	@ResponseBody
	public JsonObj questionOperation(@RequestParam("id")Integer id,@RequestParam("type")Integer type,HttpSession session){
		JsonObj jsonObj = new JsonObj();
		User user = SessionUtil.getSessionUser(session);
		if(user != null){
			question  = questionService.selectByPrimaryKey(id);
			QuestionOperation qo = new QuestionOperation();
			qo.setUserId(user.getId());
			qo.setQuestionId(id);
			qo.setOperationType(type);
			QuestionOperation q = questionOperationService.selectByOperation(qo);
			if(q != null){
				jsonObj.setSuccess(false);
				jsonObj.setCode(103);
				jsonObj.setMessage("你已经"+(type==1?"关注":"收藏")+"过，请勿重复操作!");
			}else{
					qo.setCreateDate(new Date());
					questionOperationService.insertSelective(qo);
					if(type ==2){//收藏
						question.setQuestionCollection(question.getQuestionCollection()+1);
					}else{
						question.setQuestionAbout(question.getQuestionAbout()+1);
					}
					boolean success= questionService.updateByPrimaryKeySelective(question);
					if(success){
						Question question  = questionService.selectByPrimaryKey(id);
						jsonObj.setObject(question);
						jsonObj.setSuccess(true);
						jsonObj.setCode(100);
						jsonObj.setMessage("恭喜你,"+(type==1?"关注":"收藏")+"成功!");
					}else{
						jsonObj.setSuccess(false);
						jsonObj.setMessage("对不起，你"+(type==1?"关注":"收藏")+"失败!");
						jsonObj.setCode(101);
					}
			}
		}else{
			jsonObj.setSuccess(false);
			jsonObj.setCode(102);
			jsonObj.setMessage("你还未登录，请登录后再操作!");
		}
		return jsonObj;
	}
	
	/**
	 * 回答提问
	 */
	@RequestMapping("questionAnswer")
	@ResponseBody
	public JsonObj questionAnswer(@RequestParam("questionAnswer") String questionAnswer,@RequestParam("questionId") Integer questionId,HttpServletRequest request,HttpSession session){
		JsonObj json = new JsonObj();
		User user = (User) session.getAttribute("user");
		if(user != null){
			QuestionAnswer qa = new QuestionAnswer();
			qa.setAnswerUserId(user.getId());
			qa.setQuestionId(questionId);
			QuestionAnswer answer = questionAnswerService.selectIsExists(qa);
			if(answer == null){
				//过滤敏感词
				List<SensitiveWord> words = sensitiveWordService.selectAll();
				for (SensitiveWord word : words) {
					questionAnswer = questionAnswer.replaceAll(word.getWord(), "***");
				}
				qa.setQuestionAnswer(questionAnswer);
				qa.setCreateDate(new Date());
				boolean success = questionAnswerService.insertSelective(qa);
				String msg= "回答成功!";
				if(success){
					json.setSuccess(success);
					//回答数量加1
					Question question = questionService.selectByPrimaryKey(questionId);
					question.setQuestionAnswerCount(question.getQuestionAnswerCount()+1);
					questionService.updateByPrimaryKeySelective(question);
				}else{
					msg = "回答失败!";
					success = false;
					json.setCode(102);
				}
				json.setObject(qa);
				json.setSuccess(success);
				json.setMessage(msg);
			}else{
				json.setSuccess(false);
				json.setMessage("你已回答过，请勿重复回答!");
				json.setCode(101);
			}
		}else{
			json.setSuccess(false);
			json.setMessage("对不起，请登陆之后再回答!");
			json.setCode(100);
		}
		return json;
	}
	
}
